package com.jinlia.show.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinlia.show.modules.base.constant.DeliveryStatusEnum;
import com.jinlia.show.modules.base.constant.DeliveryTypeEnum;
import com.jinlia.show.modules.base.constant.ErrandOrderStatusEnum;
import com.jinlia.show.modules.base.constant.OrderStatusEnum;
import com.jinlia.show.modules.base.model.po.ErrandOrderPO;
import com.jinlia.show.modules.base.model.po.OrderPO;
import com.jinlia.show.modules.base.model.query.DeliveryOrderQuery;
import com.jinlia.show.modules.base.model.query.OrderQuery;
import com.jinlia.show.modules.base.model.vo.DeliveryOrderVO;
import com.jinlia.show.modules.system.service.WorktableService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/worktable")
public class WorktableController {
    @Autowired
    private WorktableService worktableService;

    @PostMapping("/todo/saleOrder2")
    public JSONObject getTodoSaleOrder2(@RequestBody OrderQuery orderQuery) {
        if (orderQuery.getOrderDate() == null) {
            orderQuery.setOrderDate(LocalDate.now());
        }
        List<OrderPO> todoSaleOrder2 = worktableService.getTodoSaleOrder2(orderQuery);
        //构造数据
        JSONObject jsonObject = new JSONObject();
        List<OrderPO> collectWaitConfirm = todoSaleOrder2.stream()
                .filter(i -> OrderStatusEnum.STEP_0.getValue().equals(i.getOrderStatus())).collect(Collectors.toList());
        jsonObject.put("waitConfirm", collectWaitConfirm);//待确认
        List<OrderPO> collectMaking = todoSaleOrder2.stream()
                .filter(i -> OrderStatusEnum.STEP_1.getValue().equals(i.getOrderStatus())).collect(Collectors.toList());
        jsonObject.put("making", collectMaking);//制配中
        List<OrderPO> inStoreOrder = todoSaleOrder2.stream()
                .filter(i -> {
                    if (OrderStatusEnum.STEP_0.getValue().equals(i.getOrderStatus()) || OrderStatusEnum.STEP_1.getValue().equals(i.getOrderStatus())) {
                        return false;
                    }
                    if (DeliveryTypeEnum.SELF_PICT.getValue().equals(i.getDeliveryType())) {
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
        jsonObject.put("inStoreOrder", inStoreOrder);//堂食订单
        List<OrderPO> outStoreOrder = todoSaleOrder2.stream()
                .filter(i -> {
                    if (OrderStatusEnum.STEP_0.getValue().equals(i.getOrderStatus()) || OrderStatusEnum.STEP_1.getValue().equals(i.getOrderStatus())) {
                        return false;
                    }
                    if (!DeliveryTypeEnum.SELF_PICT.getValue().equals(i.getDeliveryType())) {
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
        jsonObject.put("outStoreOrder", outStoreOrder);//
        return jsonObject;
    }

    /**
     * 跑腿待办
     */
    @GetMapping("/todo/errandOrder")
    public JSONObject getTodoErrandOrder() {
        List<ErrandOrderPO> todoSaleOrder = worktableService.getTodoErrandOrder();
        //构造数据
        JSONObject jsonObject = new JSONObject();
        List<ErrandOrderPO> collectA = todoSaleOrder.stream()
                .filter(i -> i.getErrandOrderStatus().equals(ErrandOrderStatusEnum.A.getValue())).collect(Collectors.toList());
        jsonObject.put("A", collectA);
        jsonObject.put("ACount", collectA.size());
        List<ErrandOrderPO> collectB = todoSaleOrder.stream()
                .filter(i -> i.getErrandOrderStatus().equals(ErrandOrderStatusEnum.B.getValue())).collect(Collectors.toList());
        jsonObject.put("B", collectB);
        jsonObject.put("BCount", collectB.size());
        List<ErrandOrderPO> collectI = todoSaleOrder.stream()
                .filter(i -> i.getErrandOrderStatus().equals(ErrandOrderStatusEnum.I.getValue())).collect(Collectors.toList());
        jsonObject.put("I", collectI);
        jsonObject.put("IDeliveryCount", collectI.size());
        return jsonObject;
    }

    /**
     * 跑腿待办
     */
    @GetMapping("/todo/errandOrder2")
    public List<ErrandOrderPO> getTodoErrandOrder2() {
        List<ErrandOrderPO> todoSaleOrder = worktableService.getTodoErrandOrder();
        return todoSaleOrder;
    }

    /**
     * 待确认订单
     */
    @PostMapping("/todo/confirmedOrder")
    public List<OrderPO> getTodoConfirmedOrder(@RequestBody OrderQuery orderQuery) {
        return  worktableService.getTodoConfirmedOrder(orderQuery);
    }

    /**
     *
     * @param orderQuery
     * @return
     */
    @PostMapping("/todo/deliveryOrder")
    public List<OrderPO> getTodoDeliveryOrder(@RequestBody OrderQuery orderQuery) {
        return  worktableService.getTodoDeliveryOrder(orderQuery);
    }
}
